from typing import List, Optional
from app.schemas.base import IDModelMixin
from app.exceptions.base import NotFoundException


class ExampleItem(IDModelMixin):
    """示例项目数据模型"""
    name: str
    description: Optional[str] = None
    value: float


class ExampleService:
    """示例服务类，提供示例业务逻辑"""
    
    def __init__(self):
        # 这里使用内存存储作为示例
        # 实际项目中应该使用数据库
        self._items = {}
        self._next_id = 1
    
    def get_all(self) -> List[ExampleItem]:
        """获取所有示例项目"""
        return list(self._items.values())
    
    def get_by_id(self, item_id: int) -> ExampleItem:
        """根据ID获取示例项目"""
        item = self._items.get(item_id)
        if not item:
            raise NotFoundException(f"示例项目 {item_id} 不存在")
        return item
    
    def create(self, name: str, value: float, description: Optional[str] = None) -> ExampleItem:
        """创建新的示例项目"""
        item = ExampleItem(
            id=self._next_id,
            name=name,
            value=value,
            description=description
        )
        self._items[self._next_id] = item
        self._next_id += 1
        return item
    
    def update(self, item_id: int, name: Optional[str] = None, value: Optional[float] = None, 
               description: Optional[str] = None) -> ExampleItem:
        """更新示例项目"""
        item = self.get_by_id(item_id)
        if name is not None:
            item.name = name
        if value is not None:
            item.value = value
        if description is not None:
            item.description = description
        self._items[item_id] = item
        return item
    
    def delete(self, item_id: int) -> None:
        """删除示例项目"""
        item = self.get_by_id(item_id)
        del self._items[item_id]


# 创建服务实例，供其他模块导入使用
example_service = ExampleService()